#include "selectemployeeinfoview.h"
#include <QDebug>
#include <QMessageBox>
#include "employeemanager.h"

SelectEmployeeInfoView::SelectEmployeeInfoView(EmployeeManager *employeeManager, QWidget *parent) : QWidget(parent),
    m_employeeManager(employeeManager)
{
    // 设置窗口标题
    setWindowTitle("员工个人信息");
    setMinimumSize(400, 300);

    // 创建信息显示区域
    QLabel *infoTitleLabel = new QLabel("员工信息:", this);
    QFont titleFont;
    titleFont.setBold(true);
    infoTitleLabel->setFont(titleFont);

    empIdLabel = new QLabel(this);
    empNameLabel = new QLabel(this);
    genderLabel = new QLabel(this);
    positionLabel = new QLabel(this);
    deptNameLabel = new QLabel(this);

    // 设置布局
    infoLayout = new QGridLayout;
    infoLayout->addWidget(new QLabel("员工ID:", this), 0, 0);
    infoLayout->addWidget(empIdLabel, 0, 1);
    infoLayout->addWidget(new QLabel("员工姓名:", this), 1, 0);
    infoLayout->addWidget(empNameLabel, 1, 1);
    infoLayout->addWidget(new QLabel("性别:", this), 2, 0);
    infoLayout->addWidget(genderLabel, 2, 1);
    infoLayout->addWidget(new QLabel("职位:", this), 3, 0);
    infoLayout->addWidget(positionLabel, 3, 1);
    infoLayout->addWidget(new QLabel("部门:", this), 4, 0);
    infoLayout->addWidget(deptNameLabel, 4, 1);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(infoTitleLabel);
    mainLayout->addLayout(infoLayout);
    mainLayout->addStretch();

    setLayout(mainLayout);
}



void SelectEmployeeInfoView::setEmployeeId(const QString &employeeId)
{
    m_employeeId = employeeId;
    onQueryButtonClicked(); // 自动查询
}

void SelectEmployeeInfoView::onQueryButtonClicked()
{
    if (m_employeeId.isEmpty()) {
        QMessageBox::warning(this, "警告", "员工ID为空");
        return;
    }

    // 查询员工信息
    QMap<QString, QString> employeeInfo = m_employeeManager->getEmployeeInfo(m_employeeId);

    if (employeeInfo.isEmpty()) {
        QMessageBox::information(this, "提示", "未找到该员工信息");
        // 清空显示
        displayEmployeeInfo(QMap<QString, QString>());
    } else {
        // 显示员工信息
        displayEmployeeInfo(employeeInfo);
    }
}

void SelectEmployeeInfoView::displayEmployeeInfo(const QMap<QString, QString> &info)
{
    if (info.isEmpty()) {
        empIdLabel->clear();
        empNameLabel->clear();
        genderLabel->clear();
        positionLabel->clear();
        deptNameLabel->clear();
    } else {
        empIdLabel->setText(info.value("emp_id", ""));
        empNameLabel->setText(info.value("emp_name", ""));
        genderLabel->setText(info.value("gender", ""));
        positionLabel->setText(info.value("position", ""));
        deptNameLabel->setText(info.value("dept_name", ""));
    }
}

SelectEmployeeInfoView::~SelectEmployeeInfoView()
{
    // 不需要手动删除m_employeeManager，它由外部传入并管理
}
